/*
 * @Author: Sam
 * @Date: 2025-05-07 10:01:17
 * @LastEditTime: 2025-07-17 09:14:43
 * @LastEditors: Sam
 */

export interface TextTextureProps {
	width: number;
	height: number;
	lineWidth: number;
	lineColor: string;
	blur: number;
	blurColor: string;
}

export class Utils {
	static createOutlineCanvas(props: TextTextureProps) {
		const canvas = document.createElement('canvas');
		canvas.width = props.width;
		canvas.height = props.height;
		canvas.style.width = props.width + 'px';
		canvas.style.height = props.height + 'px';
		canvas.style.position = 'absolute';
		canvas.style.left = '0';
		canvas.style.top = '0';
		canvas.style.zIndex = '100';
		const ctx = canvas.getContext('2d')!;
		ctx.shadowColor = props.blurColor ?? 'rgba(0, 255, 255, 0.8)';
		ctx.shadowBlur = props.blur;

		ctx.strokeStyle = props.lineColor;
		ctx.lineWidth = props.lineWidth ?? 4;
		ctx.strokeRect(0, 0, props.width, props.height);
		// document.body.append(canvas);
		return canvas;
	}

	static closestElement(elem: HTMLElement , target: HTMLElement ) {
		while (elem) {
			if (elem === target){
				return elem;
			}
			if(!elem.parentElement) {
				break;
			}
			elem = elem.parentElement;
		}
		return;
	}

	static defer<T>(){
		let resolveFn: (value: T) => void;
		const globalReadyPromise = new Promise<T>((resolve) => {
			resolveFn = resolve;
		});
		return {promise:globalReadyPromise, resolve:resolveFn!};
	}

	
}